<!-- 多纵坐标图 -->
<template>
  <div id="muti-y-hetong-business" style="height: 100%; width: 100%"></div>
</template>

<script setup>
import * as echarts from 'echarts';
import { reactive, onMounted, watch } from 'vue';

const props = defineProps({
  amounts: {
    type: Array,
    default() {
      return [];
    }
  },
  counts: {
    type: Array,
    default() {
      return [];
    }
  },
})

const colors = ['#5470C6', '#EE6666'];
const option = reactive({
  color: colors,
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross'
    }
  },
  grid: {
    right: '10%',
    top: '20%',
    bottom: '15%'
  },
  legend: {
    data: ['金额', '数量']
  },
  xAxis: [
    {
      type: 'category',
      axisTick: {
        alignWithLabel: true
      },
      axisLabel: {
        interval: 0
      },
      // prettier-ignore
      data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '金额（万元）',
      position: 'right',
      alignTicks: true,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[0]
        }
      },
      axisLabel: {
        formatter: '{value}'
      }
    },
    {
      type: 'value',
      name: '数量（个）',
      position: 'left',
      alignTicks: true,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[0]
        }
      },
      axisLabel: {
        formatter: '{value}'
      }
    }
  ],
  series: [
    {
      name: '金额',
      type: 'bar',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' 万元';
        }
      },
      data: [120, 132, 101, 134],
      label: {
        show: true,
        position: 'top',
        formatter: '{c}',
        z: 10 // 设置标注的层级
      },
    },
    {
      name: '数量',
      type: 'line',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' 个';
        }
      },
      yAxisIndex: 1,
      data: [23, 21, 15, 17]
    }
  ]
})
let mutiy = null;

watch(() => props, () => {
  console.log('props', props);
  option.series[0].data = props.amounts;
  option.series[1].data = props.counts;
  mutiy.setOption(option);
}, { deep: true })

onMounted(() => {
  mutiy = echarts.init(document.getElementById("muti-y-hetong-business"));
  option && mutiy.setOption(option);
});
</script>

<style scoped></style>
